﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraMessageBox;
using System.Reflection;
using Infragistics.Win.UltraWinTree;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace WindowsFormApplication.FormBase
{
    public partial class frmBaseTreeView : Form
    {
        public enum ActionMode
        {
            AddNew = 0,
            Edit = 1,
            Delete = 2,
            None = 3
        }

        public string FormDetail { get; set; }

        public string NamespaceForm { get; set; }

        public string TableName { get; set; }

        public string TablePrimaryKey { get; set; }

        public string TableNameTree { get; set; }

        public string TreePrimaryKey { get; set; }

        public string TreeParentKey { get; set; }

        public string RelationName { get; set; }

        public ActionMode eActionMode { get; set; }

        public string DisplayFieldTree { get; set; }

        #region function override

        //ham khoi tao du lieu
        protected virtual void InitData()
        {

        }
        //ham show formdetail
        protected virtual void ShowFormDetail()
        {
            frmBaseTreeViewDetail frmDetail = new frmBaseTreeViewDetail();
            frmDetail = GetFormDetail();
            frmDetail.eActionMode = (frmBaseTreeViewDetail.ActionMode)eActionMode;
            if (frmDetail.eActionMode == frmBaseTreeViewDetail.ActionMode.AddNew)
                frmDetail.KeyValue = null;
            else
                frmDetail.KeyValue = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
            if (frmDetail.ShowDialog() == DialogResult.OK) { }
            frmDetail.Dispose();
            InitData();
        }

        protected virtual void DeleteRow()
        {
        }

        protected virtual void DeleteSelectedRow()
        {
            DialogResult result = ShowMessageBoxReusult("Bạn có muốn xóa dòng dữ liệu này không ?", "Xóa dữ liệu danh mục !", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                DeleteRow();
                InitData();
            }
            else if (result == System.Windows.Forms.DialogResult.Cancel)
                return;
        }

        protected virtual void EditData()
        {
            if (grdMain.ActiveRow != null)
            {
                eActionMode = ActionMode.Edit;
                ShowFormDetail();
            }
        }

        protected virtual void AddData()
        {
            eActionMode = ActionMode.AddNew;
            ShowFormDetail();
        }

        protected virtual void ShowMessageBox(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            using (UltraMessageBoxInfo ultraMessageBoxInfo = new UltraMessageBoxInfo())
            {
                ultraMessageBoxInfo.Text = message;
                ultraMessageBoxInfo.Caption = "Hệ thống quản lý nhà hàng- Nhóm phát triển phần mềm HUMG";
                ultraMessageBoxInfo.Header = header;
                ultraMessageBoxInfo.Footer = "Hệ thống được phát triển bởi nhóm phát triển phần mềm CNPM53- HUMG";
                ultraMessageBoxInfo.Buttons = buttons;
                ultraMessageBoxInfo.Icon = icon;

                messageBoxManager.ShowMessageBox(ultraMessageBoxInfo);
            }
        }

        protected virtual DialogResult ShowMessageBoxReusult(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            using (UltraMessageBoxInfo ultraMessageBoxInfo = new UltraMessageBoxInfo())
            {
                ultraMessageBoxInfo.Text = message;
                ultraMessageBoxInfo.Caption = "Hệ thống quản lý nhà hàng- Nhóm phát triển phần mềm HUMG";
                ultraMessageBoxInfo.Header = header;
                ultraMessageBoxInfo.Footer = "Hệ thống được phát triển bởi nhóm phát triển phần mềm CNPM53- HUMG";
                ultraMessageBoxInfo.Buttons = buttons;
                ultraMessageBoxInfo.Icon = icon;

                //messageBoxManager.ShowMessageBox(ultraMessageBoxInfo);

                DialogResult result = messageBoxManager.ShowMessageBox(ultraMessageBoxInfo);

                if (result == System.Windows.Forms.DialogResult.OK)
                    return DialogResult.OK;
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                    return DialogResult.Cancel;
                else
                    return DialogResult.Retry; //chua xu ly het
            }
        }

        protected virtual void NextPage()
        {
        }

        protected virtual void PreviousPage()
        {
        }

        protected virtual frmBaseTreeViewDetail GetFormDetail()
        {
            try
            {
                frmBaseTreeViewDetail frmDetail = new frmBaseTreeViewDetail();
                frmMain mainForm = new frmMain();
                Type typeOfForm = Assembly.GetExecutingAssembly().GetType(NamespaceForm + "." + FormDetail);
                if (typeOfForm != null)
                    frmDetail = (frmBaseTreeViewDetail)Activator.CreateInstance(typeOfForm);
                else
                    frmDetail = null;
                return frmDetail;
            }
            catch (Exception ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; }

        }

        protected virtual void FilterObject(string keyValue)
        {

        }

        #endregion

        private void SetRowPositionAfterUpdate()
        {
            if (grdMain.Rows.Count > 0)
            {
                grdMain.Selected.Rows.Clear();
                grdMain.Rows[0].Activate();
            }
        }

        public frmBaseTreeView()
        {
            InitializeComponent();
        }

        private void toolbarsManager_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            try
            {
                switch (e.Tool.Key)
                {
                    case "mnuAddNew":
                        AddData();
                        SetRowPositionAfterUpdate();
                        break;
                    case "mnuEdit":
                        EditData();
                        SetRowPositionAfterUpdate();
                        break;
                    case "mnuDelete":
                        DeleteSelectedRow();
                        break;
                    case "mnuRefresh":
                        InitData();
                        break;
                    case "mnuExit":
                        this.Close();
                        break;
                    case "mnuPrevious":
                        PreviousPage();
                        break;
                    case "mnuNext":
                        NextPage();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void grdMain_DoubleClick(object sender, EventArgs e)
        {
            if (grdMain.ActiveRow.Cells[TablePrimaryKey].Value != null && grdMain.ActiveRow.Cells[TablePrimaryKey].Value != "")
            {
                EditData();
                SetRowPositionAfterUpdate();
            }
        }

        private void frmBaseTreeView_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void grdMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            grdMain.DisplayLayout.Bands[0].Summaries.RemoveAt(0);

            SummarySettings summary = e.Layout.Bands[0].Summaries.Add(SummaryType.Count, e.Layout.Bands[0].Columns[0]);
            summary.DisplayFormat = "Số bản ghi = {0}";
            summary.Appearance.TextHAlign = HAlign.Left;
            summary.Appearance.BackColor = Color.FromArgb(215, 215, 215);
            summary.Appearance.ForeColor = Color.FromArgb(22, 96, 179);
            summary.SummaryDisplayArea = SummaryDisplayAreas.BottomFixed | SummaryDisplayAreas.GroupByRowsFooter;
        }

        private void treeMain_InitializeDataNode(object sender, InitializeDataNodeEventArgs e)
        {
            if (e.Node.Parent == null && (!object.ReferenceEquals(e.Node.Cells[TreeParentKey].Value, DBNull.Value)))
            {
                e.Node.Visible = false;
                return;
            }

            if (e.Node.Nodes.Count > 0)
            {
                if (e.Node.Parent == null) { e.Node.Override.NodeSpacingBefore = 5; }
                e.Node.Override.NodeAppearance.FontData.Bold = Infragistics.Win.DefaultableBoolean.True;
            }

            if (e.Node.Parent == null) { e.Node.Override.NodeAppearance.FontData.Bold = Infragistics.Win.DefaultableBoolean.True; }
            e.Node.ExpandAll(Infragistics.Win.UltraWinTree.ExpandAllType.Always);

            if (e.Node.LeftImages.Count == 0)
            {
                if (e.Node.Level < 4)
                {
                    e.Node.LeftImages.Add(imgTreeMain.Images[e.Node.Level]);
                }
            }
        }

        private void treeMain_ColumnSetGenerated(object sender, ColumnSetGeneratedEventArgs e)
        {
            e.ColumnSet.NodeTextColumn = e.ColumnSet.Columns[DisplayFieldTree];
        }

        private void treeMain_AfterSelect(object sender, SelectEventArgs e)
        {
            if (treeMain.SelectedNodes != null)
                FilterObject(treeMain.ActiveNode.DataKey.ToString());
        }

        private void treeMain_DoubleClick(object sender, EventArgs e)
        {
            AddData();
            SetRowPositionAfterUpdate();
        }
    }
}
